const path = require('path')

const {
  override,
  addWebpackAlias,
  addLessLoader,
  adjustStyleLoaders,
  addDecoratorsLegacy,
  fixBabelImports,
  overrideDevServer,
} = require('customize-cra')

const SAPI = process.env.REACT_APP_SAPI

const addProxy = config => {
  console.log(process.env)
  return {
    ...config,
    proxy: {
      '/sapi': {
        target: 'http://shop.fenotes.com',
        changeOrigin: true,
        pathRewrite: {
          '^/sapi': '/adminapi',
        },
      },
      '/api': {
        target: 'http://www.google.com',
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/capi',
        },
      },
      '/dapi': {
        target: 'https://fenote.com',
        changeOrigin: true,
      },
    },
  }
}

module.exports = {
  webpack: override(
    addDecoratorsLegacy(),
    fixBabelImports('import', {
      libraryName: 'antd',
      libraryDirectory: 'es',
      style: true,
    }),
    addLessLoader({
      lessOptions: {
        javascriptEnabled: true,
      },
    }),
    adjustStyleLoaders(({ use: [, , postcss] }) => {
      const postcssOptions = postcss.options
      postcss.options = { postcssOptions }
    }),
    addWebpackAlias({
      '@': path.resolve(__dirname, 'src'),
    })
  ),
  devServer: overrideDevServer(addProxy),
}
